package com.plugins.pluginlib.internal;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.os.Build;
import android.os.Bundle;

import com.plugins.pluginlib.IPlugin;
import com.plugins.pluginlib.utils.IConfigs;
import com.plugins.pluginlib.utils.IConstants;

import java.lang.reflect.Constructor;

/**
 *插件activity代理类
 *
 */
public class IProxyImpl {

    private static final String TAG = "IProxyImpl";

    private String mClass;
    private String mPackageName;
    private String mExtraPackageName;
    private String mExtraData;

    private IPluginPackage mPluginPackage;
    private IPluginManager mPluginManager;

    private AssetManager mAssetManager;
    private Resources mResources;
    private Theme mTheme;

    private ActivityInfo mActivityInfo;
    private Activity mProxyActivity;
    protected IPlugin mPluginActivity;
    public ClassLoader mPluginClassLoader;

    public IProxyImpl(Activity activity) {
        mProxyActivity = activity;
    }

    private void initializeActivityInfo() {
        PackageInfo packageInfo = mPluginPackage.packageInfo;
        if ((packageInfo.activities != null) && (packageInfo.activities.length > 0)) {
            if (mClass == null) {
                mClass = packageInfo.activities[0].name;
            }

            //Finals 修复主题BUG
            int defaultTheme = packageInfo.applicationInfo.theme;
            for (ActivityInfo a : packageInfo.activities) {
                if (a.name.equals(mClass)) {
                    mActivityInfo = a;
                    // Finals ADD 修复主题没有配置的时候插件异常
                    if (mActivityInfo.theme == 0) {
                        if (defaultTheme != 0) {
                            mActivityInfo.theme = defaultTheme;
                        } else {
                            if (Build.VERSION.SDK_INT >= 14) {
                                mActivityInfo.theme = android.R.style.Theme_DeviceDefault;
                            } else {
                                mActivityInfo.theme = android.R.style.Theme;
                            }
                        }
                    }
                }
            }

        }
    }

    private void handleActivityInfo() {
        if (mActivityInfo.theme > 0) {
            mProxyActivity.setTheme(mActivityInfo.theme);
        }
        Theme superTheme = mProxyActivity.getTheme();
        mTheme = mResources.newTheme();
        mTheme.setTo(superTheme);
        try {
            mTheme.applyStyle(mActivityInfo.theme, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // TODO: handle mActivityInfo.launchMode here in the future.
    }

    public void onCreate(Intent intent) {
        intent.setExtrasClassLoader(IConfigs.sPluginClassloader);
        mPackageName = intent.getStringExtra(IConstants.EXTRA_PACKAGE);
        mClass = intent.getStringExtra(IConstants.EXTRA_CLASS);

        mExtraPackageName = intent.getStringExtra(IConstants.EXTRA_PACKNAME);
        mExtraData = intent.getStringExtra(IConstants.EXTRA_FROM_HOST_MESSAGE);

        mPluginManager = IPluginManager.getInstance(mProxyActivity);
        mPluginPackage = mPluginManager.getPackage(mPackageName);
        mAssetManager = mPluginPackage.assetManager;
        mResources = mPluginPackage.resources;

        initializeActivityInfo();
        handleActivityInfo();
        launchTargetActivity();
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    protected void launchTargetActivity() {
        try {
            Class<?> localClass = getClassLoader().loadClass(mClass);
            Constructor<?> localConstructor = localClass.getConstructor(new Class[] {});
            Object instance = localConstructor.newInstance(new Object[] {});
            mPluginActivity = (IPlugin) instance;
            ((IAttachable) mProxyActivity).attach(mPluginActivity, mPluginManager);
            mPluginActivity.attach(mProxyActivity, mPluginPackage);

            Bundle bundle = new Bundle();
            bundle.putInt(IConstants.FROM, IConstants.FROM_EXTERNAL);
            bundle.putString(IConstants.EXTRA_FROM_HOST_MESSAGE,mExtraData);
            bundle.putString(IConstants.EXTRA_PACKNAME,mExtraPackageName);
            mPluginActivity.onCreate(bundle);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public ClassLoader getClassLoader() {
        return mPluginPackage.classLoader;
    }

    public AssetManager getAssets() {
        return mAssetManager;
    }

    public Resources getResources() {
        return mResources;
    }

    public Theme getTheme() {
        return mTheme;
    }

    public IPlugin getRemoteActivity() {
        return mPluginActivity;
    }
}
